3 use Wikimedia\Rdbms\TransactionProfiler
;
4 use Wikimedia\Rdbms\DatabaseDomain
;
5 use Wikimedia\Rdbms\Database
;
8 * Helper for testing the methods from the Database class
11 class DatabaseTestHelper
extends Database
{
14 * __CLASS__ of the test suite,
15 * used to determine, if the function name is passed every time to query()
17 protected $testName = [];
20 * Array of lastSqls passed to query(),
21 * This is an array since some methods in Database can do more than one
22 * query. Cleared when calling getLastSqls().
24 protected $lastSqls = [];
26 /** @var array List of row arrays */
27 protected $nextResult = [];
29 /** @var array|null */
30 protected $nextError = null;
31 /** @var array|null */
32 protected $lastError = null;
35 * Array of tables to be considered as existing by tableExist()
36 * Use setExistingTables() to alter.
38 protected $tablesExists;
41 * Value to return from unionSupportsOrderAndLimit()
43 protected $unionSupportsOrderAndLimit = true;
45 public function __construct( $testName, array $opts = [] ) {
46 $this->testName
= $testName;
48 $this->profiler
= null;
49 $this->trxProfiler
= new TransactionProfiler();
50 $this->cliMode
= $opts['cliMode'] ??
true;
51 $this->connLogger
= new \Psr\Log\
NullLogger();
52 $this->queryLogger
= new \Psr\Log\
NullLogger();
53 $this->errorLogger
= function ( Exception
$e ) {
54 wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
56 $this->deprecationLogger
= function ( $msg ) {
59 $this->currentDomain
= DatabaseDomain
::newUnspecified();
60 $this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
64 * Returns SQL queries grouped by '; '
65 * Clear the list of queries that have been done so far.
68 public function getLastSqls() {
69 $lastSqls = implode( '; ', $this->lastSqls
);
75 public function setExistingTables( $tablesExists ) {
76 $this->tablesExists
= (array)$tablesExists;
80 * @param mixed $res Use an array of row arrays to set row result
82 public function forceNextResult( $res ) {
83 $this->nextResult
= $res;
87 * @param int $errno Error number
88 * @param string $error Error text
89 * @param array $options
90 * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
92 public function forceNextQueryError( $errno, $error, $options = [] ) {
93 $this->nextError
= [ 'errno' => $errno, 'error' => $error ] +
$options;
96 protected function addSql( $sql ) {
97 // clean up spaces before and after some words and the whole string
98 $this->lastSqls
[] = trim( preg_replace(
99 '/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
104 protected function checkFunctionName( $fname ) {
105 if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
106 return; // no $fname parameter
109 // Handle some internal calls from the Database class
112 '/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
119 if ( substr( $check, 0, strlen( $this->testName
) ) !== $this->testName
) {
120 throw new MWException( 'function name does not start with test class. ' .
121 $fname . ' vs. ' . $this->testName
. '. ' .
122 'Please provide __METHOD__ to database methods.' );
126 function strencode( $s ) {
127 // Choose apos to avoid handling of escaping double quotes in quoted text
128 return str_replace( "'", "\'", $s );
131 public function addIdentifierQuotes( $s ) {
132 // no escaping to avoid handling of double quotes in quoted text
136 public function query( $sql, $fname = '', $tempIgnore = false ) {
137 $this->checkFunctionName( $fname );
139 return parent
::query( $sql, $fname, $tempIgnore );
142 public function tableExists( $table, $fname = __METHOD__
) {
143 $tableRaw = $this->tableName( $table, 'raw' );
144 if ( isset( $this->sessionTempTables
[$tableRaw] ) ) {
145 return true; // already known to exist
148 $this->checkFunctionName( $fname );
150 return in_array( $table, (array)$this->tablesExists
);
153 // Redeclare parent method to make it public
154 public function nativeReplace( $table, $rows, $fname ) {
155 parent
::nativeReplace( $table, $rows, $fname );
162 function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
163 $this->conn
= (object)[ 'test' ];
168 function fetchObject( $res ) {
172 function fetchRow( $res ) {
176 function numRows( $res ) {
180 function numFields( $res ) {
184 function fieldName( $res, $n ) {
188 function insertId() {
192 function dataSeek( $res, $row ) {
196 function lastErrno() {
197 return $this->lastError ?
$this->lastError
['errno'] : -1;
200 function lastError() {
201 return $this->lastError ?
$this->lastError
['error'] : 'test';
204 protected function wasKnownStatementRollbackError() {
205 return $this->lastError
['wasKnownStatementRollbackError'] ??
false;
208 function fieldInfo( $table, $field ) {
212 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
216 function fetchAffectedRowCount() {
220 function getSoftwareLink() {
224 function getServerVersion() {
228 function getServerInfo() {
233 return $this->conn ?
true : false;
236 function ping( &$rtt = null ) {
241 protected function closeConnection() {
245 protected function doQuery( $sql ) {
246 $sql = preg_replace( '< /\* .+? \*/>', '', $sql );
247 $this->addSql( $sql );
249 if ( $this->nextError
) {
250 $this->lastError
= $this->nextError
;
251 $this->nextError
= null;
255 $res = $this->nextResult
;
256 $this->nextResult
= [];
257 $this->lastError
= null;
259 return new FakeResultWrapper( $res );
262 public function unionSupportsOrderAndLimit() {
263 return $this->unionSupportsOrderAndLimit
;
266 public function setUnionSupportsOrderAndLimit( $v ) {
267 $this->unionSupportsOrderAndLimit
= (bool)$v;